/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AsignarAlumnoGrado.java
 *
 * Created on 16/07/2012, 02:23:42 PM
 */

package interfaz;

import Clases.Alumno;
import Clases.Ciclo;
import Clases.Gradoseccionciclo;
import Controladores.AlumnoJpaController;
import Controladores.CicloJpaController;
import Controladores.GradoJpaController;
import Controladores.GradoseccioncicloJpaController;
import Controladores.SeccionJpaController;
import escuela.conexion;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author DeLeon
 */
public class AsignarAlumnoGrado extends javax.swing.JDialog {

    conexion cnx= conexion.ObtenerInstancia();
    EntityManagerFactory emf=cnx.ObtenerEMF();
    EntityManager em=emf.createEntityManager();
    CicloJpaController _Ciclos = new CicloJpaController(emf);
    GradoseccioncicloJpaController _Clases = new GradoseccioncicloJpaController(emf);
    GradoJpaController _Grados = new GradoJpaController(emf);
    SeccionJpaController _Secciones = new SeccionJpaController(emf);
    AlumnoJpaController _Alumnos = new AlumnoJpaController(emf);
    Short selecc,seleca,seleca2;



    /** Creates new form AsignarAlumnoGrado */
    public AsignarAlumnoGrado(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Object[] datos= new Object[4];
        DefaultTableModel modelo = new DefaultTableModel();
        modelo.addColumn("id");
        modelo.addColumn("Nombre");
        modelo.addColumn("Aula");
        modelo.addColumn("Capacidad");
        List<Gradoseccionciclo> listaclases = this._Clases.findGradoseccioncicloEntities();
            for(Gradoseccionciclo i: listaclases)
            {
                datos[0]=i.getIdGradoSeccionCiclo();
                datos[1]=i.getGrado().getNombre()+" Sec."+i.getSeccion().getNombre()+" ("+i.getCiclo().getAño()+")";
                datos[2]=i.getAula();
                datos[3]=i.getCapacidad();
                modelo.addRow(datos);
            }
        this.TablaClases.setModel(modelo);
        Object[] datos2= new Object[4];
        DefaultTableModel modelo2 = new DefaultTableModel();
        modelo2.addColumn("id");
        modelo2.addColumn("Apellido");
        modelo2.addColumn("Nombre");
        modelo2.addColumn("CodigoPersonal");
        List<Alumno> listaal = this._Alumnos.findAlumnoEntities();
            for(Alumno i: listaal)
            {
                datos2[0]=i.getIdAlumno();
                datos2[1]=i.getApellidos();
                datos2[2]=i.getNombres();
                datos2[3]=i.getCodigoPersonal();
                modelo2.addRow(datos2);
            }
        this.TablaAlumno.setModel(modelo2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTaskPane1 = new com.l2fprod.common.swing.JTaskPane();
        jLabel1 = new javax.swing.JLabel();
        informacion = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaClases = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        TablaAlumno = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        SelCla = new javax.swing.JTextField();
        SelAlu = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        SelCla1 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        SelAlu1 = new javax.swing.JTextField();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Asignacion de Alumnos");

        jLabel1.setFont(new java.awt.Font("Arial", 1, 14));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Asignacion de Alumnos");
        jTaskPane1.add(jLabel1);

        informacion.setForeground(new java.awt.Color(255, 255, 255));
        informacion.setText("jLabel2");
        jTaskPane1.add(informacion);

        TablaClases.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        TablaClases.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaClasesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TablaClases);

        TablaAlumno.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        TablaAlumno.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaAlumnoMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(TablaAlumno);

        jLabel2.setText("Seleccionar Alumno :");

        jLabel3.setText("Seleccionar Clase :");

        jLabel4.setText("Clase Seleccionada:");

        SelCla.setEnabled(false);

        SelAlu.setEnabled(false);

        jLabel5.setText("Alumno Seleccionado:");

        jButton1.setText("Asignar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(jTable2);

        jButton2.setText("Desasignar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel6.setText("Clase Seleccionada:");

        SelCla1.setEnabled(false);

        jLabel7.setText("Alumno Seleccionado:");

        SelAlu1.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTaskPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(SelCla, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SelAlu, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE))
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SelCla1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SelAlu1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTaskPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(SelCla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(SelAlu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(SelCla1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(SelAlu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TablaClasesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaClasesMouseClicked
        // TODO add your handling code here:
        int filaseleccionada = this.TablaClases.getSelectedRow();
        if(filaseleccionada > -1) {
            try {
                Short idClase= (Short) this.TablaClases.getValueAt(filaseleccionada,0);
                selecc=idClase;
                this.SelCla.setText(this._Clases.findGradoseccionciclo(idClase).getGrado().getNombre()+" Sec."+this._Clases.findGradoseccionciclo(idClase).getSeccion().getNombre() +" ("+this._Clases.findGradoseccionciclo(idClase).getCiclo().getAño()+")");
                this.SelCla1.setText(this._Clases.findGradoseccionciclo(idClase).getGrado().getNombre()+" Sec."+this._Clases.findGradoseccionciclo(idClase).getSeccion().getNombre() +" ("+this._Clases.findGradoseccionciclo(idClase).getCiclo().getAño()+")");
            } catch (Exception ex) {
                Logger.getLogger(AdminCurso.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_TablaClasesMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if(seleca>0 && selecc>0)
        {
            try
            {
                Object[] datos2= new Object[4];
                DefaultTableModel modelo2 = new DefaultTableModel();
                modelo2.addColumn("id");
                modelo2.addColumn("Apellido");
                modelo2.addColumn("Nombre");
                modelo2.addColumn("CodigoPersonal");
                List<Alumno> listaal = em.createQuery("Select a From Alumno a Inner Join GradoAlumno ga on ga.idAlumno=a.idAlumno Inner Join Gradoseccionciclo gsc on gsc.idGradoseccionciclo=ga.idGradoseccionciclo").getResultList();
                    for(Alumno i: listaal)
                    {
                        datos2[0]=i.getIdAlumno();
                        datos2[1]=i.getApellidos();
                        datos2[2]=i.getNombres();
                        datos2[3]=i.getCodigoPersonal();
                        modelo2.addRow(datos2);
                    }
                this.jTable2.setModel(modelo2);
            }
            catch(Exception ex)
            {
                Logger.getLogger(AdminCurso.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Seleccione al alumno y la clase para asignar");
        }

        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void TablaAlumnoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaAlumnoMouseClicked
        // TODO add your handling code here:
        int filaseleccionada = this.TablaAlumno.getSelectedRow();
        if(filaseleccionada > -1) {
            try {
                Short idClase= (Short) this.TablaAlumno.getValueAt(filaseleccionada,0);
                seleca=idClase;
                this.SelAlu.setText(this._Alumnos.findAlumno(idClase).getApellidos()+", "+this._Alumnos.findAlumno(idClase).getNombres());
            } catch (Exception ex) {
                Logger.getLogger(AdminCurso.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_TablaAlumnoMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if(seleca2>0 && selecc>0)
        {
            try
            {
                Object[] datos2= new Object[4];
                DefaultTableModel modelo2 = new DefaultTableModel();
                modelo2.addColumn("id");
                modelo2.addColumn("Apellido");
                modelo2.addColumn("Nombre");
                modelo2.addColumn("CodigoPersonal");
                List<Alumno> listaal = em.createQuery("Select a From Alumno a Inner Join GradoAlumno ga on ga.idAlumno=a.idAlumno Inner Join Gradoseccionciclo gsc on gsc.idGradoseccionciclo=ga.idGradoseccionciclo").getResultList();
                    for(Alumno i: listaal)
                    {
                        datos2[0]=i.getIdAlumno();
                        datos2[1]=i.getApellidos();
                        datos2[2]=i.getNombres();
                        datos2[3]=i.getCodigoPersonal();
                        modelo2.addRow(datos2);
                    }
                this.jTable2.setModel(modelo2);
            }
            catch(Exception ex)
            {
                Logger.getLogger(AdminCurso.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Seleccione al alumno y la clase, para desasignar");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
        // TODO add your handling code here:
        int filaseleccionada = this.jTable2.getSelectedRow();
        if(filaseleccionada > -1) {
            try {
                Short idClase= (Short) this.jTable2.getValueAt(filaseleccionada,0);
                seleca2=idClase;
                this.SelAlu1.setText(this._Alumnos.findAlumno(idClase).getApellidos()+", "+this._Alumnos.findAlumno(idClase).getNombres());
            } catch (Exception ex) {
                Logger.getLogger(AdminCurso.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jTable2MouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AsignarAlumnoGrado dialog = new AsignarAlumnoGrado(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField SelAlu;
    private javax.swing.JTextField SelAlu1;
    private javax.swing.JTextField SelCla;
    private javax.swing.JTextField SelCla1;
    private javax.swing.JTable TablaAlumno;
    private javax.swing.JTable TablaClases;
    private javax.swing.JLabel informacion;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private com.l2fprod.common.swing.JTaskPane jTaskPane1;
    // End of variables declaration//GEN-END:variables

}
